<template>
  <div>
    <el-dialog title="店铺中在线商品" :visible.sync="open" width="80%" :before-close="close">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-form-item label="是否已绑定" prop="mergingProgramStatus">
          <el-select size="small" v-model="queryParams.mergingProgramStatus" placeholder="请选择是否已绑定套图方案"
                     clearable filterable @change="handleQuery">
            <el-option v-for="(item,index) in isBingList" :key="index"
                       :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="同步状态" prop="mergingProgramTaskStatus">
          <el-select size="small" v-model="queryParams.mergingProgramTaskStatus" placeholder="请选择同步状态"
                     clearable filterable>
            <el-option v-for="(item,index) in dict.type.merging_program_task_status" :key="index"
                       :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="商品标题" prop="subject">
          <el-input
            v-model="queryParams.subject"
            placeholder="请输入商品标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="速卖通产品Id" prop="aeProductId">
          <el-input v-model="queryParams.aeProductId" @keyup.enter.native="handleQuery"
                    placeholder="请输入速卖通产品Id搜索" clearable></el-input>
        </el-form-item>
        <el-form-item label="产品Id" prop="id">
          <el-input v-model="queryParams.id" @keyup.enter.native="handleQuery"
                    placeholder="请输入产品Id搜索" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-button type="primary" plain size="mini" :disabled="multiple" single @click="handleAdd('1')">绑定</el-button>
        <el-button type="danger" plain size="mini" :disabled="multiple" @click="handleAdd('0')">解绑</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="collectProductList" @selection-change="handleSelectionChange" stripe border>
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="产品Id" align="center" prop="id"/>
        <el-table-column label="速卖通产品ID" align="center" prop="aeProductId"/>
        <el-table-column show-overflow-tooltip label="商品标题" align="center" prop="subject"/>
        <el-table-column label="商品图" align="center" prop="white" width="100">
          <template slot-scope="scope">
            <imagePreview :src="scope.row.white" width="60px" height="60px"/>
          </template>
        </el-table-column>
        <el-table-column v-if="queryParams.mergingProgramStatus =='1' " label="套图预览" align="center" prop="white"
                         width="100">
          <template slot-scope="scope">
            <imagePreview :src="scope.row.mergingProgramName" width="60px" height="60px"/>
          </template>
        </el-table-column>
        <el-table-column label="价格" align="center" prop="productPrice"/>
        <el-table-column label="同步状态" align="center" prop="status">
          <template slot-scope="scope">
            <div v-if="scope.row.mergingProgramTaskStatus != null ">
              <dict-tag :options="dict.type.merging_program_task_status"
                        :value="scope.row.mergingProgramTaskStatus"></dict-tag>
            </div>
            <div v-else>未绑定</div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :pageSizes="[5, 10, 20]"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close('isRef')">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getSelect, getBrand} from "@/api/attribute"
import {
  selectlistCollectProductCategory
} from "@/api/gather/collectProductCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  relevanceMergingProgram,
  selectAeProductList,
  sellerStoreProductMergingProgram
} from "@/api/product/online/productSet";

export default {
  name: "GoodsList",
  dicts: ['merging_program_task_status'],
  props: {
    programId: {
      type: String,
      default: ''
    },
    storeId: {
      type: String,
      default: ''
    }
  },
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采集商品表格数据
      collectProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        id: undefined,
        categoryId: undefined,
        subject: undefined,
        collectUrl: undefined,
        skuCode: undefined,
        mergingProgramId: this.programId,
        mergingProgramStatus: '0',
        // programId: this.programId,
        storeId: this.storeId,
        productStatusType: '1',//ERP在售商品
        mergingProgramTaskStatus: undefined
      },
      isBingList: [{
        label: '是',
        value: '1'
      }, {
        label: '否',
        value: '0'
      }],
      // 标签列表
      tagList: [],
      // 类目列表
      categoryList: [],
      // 品牌列表
      brandList: []
    };
  },
  computed: {
    // 标签
    // goodsTagLabel() {
    //   if (this.tagList.length > 0) {
    //     return (row) => {
    //       if (row && row.classifyLabel) {
    //         try {
    //           const defaultList = row.classifyLabel.split(',').filter(item => !!item)
    //           const classifyLabelList = []
    //           for (let i = 0; i < defaultList.length; i++) {
    //             for (let k = 0; k < this.tagList.length; k++) {
    //               if (defaultList[i] == this.tagList[k].value) {
    //                 classifyLabelList.push(this.tagList[k].lable)
    //               }
    //             }
    //           }
    //           return classifyLabelList
    //         } catch (e) {
    //           return []
    //         }
    //       } else return []
    //     }
    //   } else return []
    // }
  },
  created() {
    this.getTagList();
    this.getGoodsCategory();
    this.getBrandList();
    this.getList()
  },
  methods: {
    // 使用方案
    useProgramme(row) {
      this.$confirm('是否确定使用此方案？').then(() => {
        sellerStoreProductMergingProgram({taskId: row.id}).then(res => {
          this.$message.success(res.msg)
        })
      })
    },
    // 关闭弹窗
    close(e) {
      this.$emit('close', e)
    },
    // 绑定商品到套图方案中
    handleAdd(e) {
      const ids = (this.ids).toString();
      if (ids) {
        let message = e == '1' ? '是否将选中的商品绑定到方案中？' : '是否将选中的产品解除绑定？'
        this.$modal.confirm(message).then(() => {
          const params = {
            type: e,
            aeProductIds: ids,
            programId: this.programId
          }
          relevanceMergingProgram(params).then(res => {
            this.getList();
            this.isImport = false;
            this.$modal.msgSuccess(res.msg);
          })
        })
      } else {
        this.$modal.msgError("请先选择商品！");
      }
    },
    // 获取店铺下在线商品列表
    getList() {
      this.loading = true;
      selectAeProductList(this.queryParams).then(res => {
        this.collectProductList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    // 标签列表
    getTagList() {
      getSelect().then(res => {
        this.tagList = res.data
      });
    },
    // 商品类目
    getGoodsCategory() {
      selectlistCollectProductCategory().then(response => {
        this.categoryList = this.handleTree(response.data, "id", "parentId");
      });
    },
    /** 转换采集商品类目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.mergingProgramId = undefined
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.aeProductId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 获取品牌选择列表
    getBrandList() {
      getBrand().then(res => {
        this.brandList = res.data
      });
    }
  }
};
</script>

<style lang="scss">
</style>

